package com.example.demo.lettuce;

//import io.lettuce.core.RedisClient;
//import io.lettuce.core.RedisURI;
//import io.lettuce.core.TrackingArgs;
//import io.lettuce.core.api.StatefulRedisConnection;
//import io.lettuce.core.api.sync.RedisCommands;
//import io.lettuce.core.pubsub.RedisPubSubAdapter;
//import io.lettuce.core.pubsub.StatefulRedisPubSubConnection;
//import io.lettuce.core.pubsub.api.sync.RedisPubSubCommands;
//import io.lettuce.core.support.caching.CacheAccessor;
//import io.lettuce.core.support.caching.CacheFrontend;
//import io.lettuce.core.support.caching.ClientSideCaching;

//import java.time.Duration;
//import java.time.temporal.ChronoUnit;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: demo
 * @description:
 * @author: zt22328 赵儒定
 * @create: 2022-04-26 14:54
 **/
public class LettuceTest {

    public static void main(String[] args) throws InterruptedException {

//        redisClientCache();
        redisSecKill();
    }

    private static void redisSecKill() {

    }

//    /**
//     * 客户端缓存
//     * @throws InterruptedException
//     */
//    private static void redisClientCache() throws InterruptedException {
//        RedisURI redisUri = RedisURI.builder()
//                .withHost("192.168.56.105")
//                .withPort(6379)
//                // 注意这里只能是0号库
//                .withDatabase(0)
//                .withTimeout(Duration.of(10, ChronoUnit.SECONDS))
//                .build();
//        RedisClient redisClient = RedisClient.create(redisUri);
//        StatefulRedisConnection<String, String> otherParty = redisClient.connect();
//        RedisCommands<String, String> commands = otherParty.sync();
//        StatefulRedisConnection<String, String> connection = redisClient.connect();
//// <2> 创建缓存访问器
//        Map<String, String> clientCache = new ConcurrentHashMap<>(); //map 自动保存所有操作key的 key=value
//        CacheFrontend<String, String> frontend = ClientSideCaching.enable(CacheAccessor.forMap(clientCache),
//                connection,
//                TrackingArgs.Builder.enabled());
//
//// <3> 客户端正常写入测试数据 k1 v1
//        String key = "k1";
//        commands.set(key, "v1");
//// <4> 循环读取
//        while (true) {
//            // <4.1> 缓存访问器中的值，查看是否和 Redis 服务端同步
//            String cachedValue = frontend.get(key);
//            System.out.println("当前 k1 的值为:--->" + cachedValue);
//            Thread.sleep(3000);
//            commands.set(key, UUID.randomUUID().toString());
//        }
//    }
}
